package tests;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class PrepareTransparentLeafs {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		File reducedDB = new File ("reducedDB");
		try {
			reducedDB.mkdir();
			File db = new File ("Leaves(ICL)");
			File[] species = db.listFiles();
			for (int i = 0; i < species.length; i++) {
				if (species[i].isDirectory()) {
					File newDir = new File("reducedDB/"+species[i].getName());
					newDir.mkdir();
					File[] photos = species[i].listFiles();
					copy(photos[0].getPath(), newDir.getPath()+"/"+photos[0].getName());
				}
			}


		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private static void copy (String input, String output) {
		try{
	          File f1 = new File(input);
	          File f2 = new File(output);
	          InputStream in = new FileInputStream(f1);

	          //For Append the file.
	          //OutputStream out = new FileOutputStream(f2,true);

	          //For Overwrite the file.
	          OutputStream out = new FileOutputStream(f2);

	          byte[] buf = new byte[1024];
	          int len;
	          while ((len = in.read(buf)) > 0){
	            out.write(buf, 0, len);
	          }
	          in.close();
	          out.close();
	          System.out.println("File copied.");
	        }
	        catch(FileNotFoundException ex){
	          System.err.println(ex.getMessage());
	          System.exit(0);
	        }
	        catch(IOException e){
	          System.err.println(e.getMessage());      
	        }
	}
}
